package com.spn.blog.api.controller.article;


import com.spn.blog.api.common.aop.LogAnnotation;
import com.spn.blog.api.common.cache.Cache;
import com.spn.blog.common.dao.pojo.LoginUser;
import com.spn.blog.common.dao.pojo.SysUser;
import com.spn.blog.common.vo.Result;
import com.spn.blog.common.vo.params.ArticleParam;
import com.spn.blog.common.vo.params.PageParams;
import com.spn.blog.service.article.ArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

//json数据进行交互
@Api(tags = "文章模块")
@RestController
@RequestMapping("api/articles")
public class ArticleController {

    @DubboReference
    private ArticleService articleService;

    @ApiOperation(value = "文章移动到ES（禁止访问）")
    @GetMapping("test")
    public Result test(){
        System.out.println("方法执行了");
        return Result.success("方法执行了");
    }

    @ApiOperation(value = "文章移动到ES（禁止访问）")
    @GetMapping("moveArticleToES")
    @PreAuthorize("hasAuthority('dev:system')")
    public Result moveArticleToES(){
        return articleService.moveArticleToES();
    }


    @ApiOperation(value = "搜索文章")
    @PostMapping("search")
    public Result search(@RequestBody ArticleParam articleParam) {
        // 写一个搜索接口
        String search = articleParam.getSearch();
        return articleService.searchArticle(search);
    }

    @ApiOperation(value = "搜索文章(未审核)")
    @PostMapping("getArticleReview")
    public Result getArticleReview(@RequestBody PageParams pageParams) {
        return articleService.getArticleReview(pageParams.getPage(), pageParams.getPageSize());
    }

    @ApiOperation(value = "审核")
    @PostMapping("isReview/{articleId}/{flag}")
    public Result isReview(@PathVariable("articleId") String articleId,@PathVariable("flag") Boolean flag) {
        return articleService.isReview(articleId, flag);
    }

    @ApiOperation(value = "我的文章")
    @PostMapping("getMyArticle")
    public Result getMyArticle(@RequestBody Map<String, Object> params) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        String status = (String)params.get("status");
        return articleService.getMyArticle(status, loginUser);
    }

    @ApiOperation(value = "搜索文章（高亮）")
    @PostMapping("searchHighLight")
    public Result searchHighLight(@RequestBody ArticleParam articleParam) {
        // 写一个搜索接口
        String search = articleParam.getSearch();
        return articleService.searchHighLight(search);
    }

    /**
     * 首页 文章列表
     * @param pageParams
     * @return
     */
    @ApiOperation(value = "获取 - 文章列表")
    @PostMapping
    //加上此注解，代表要对此接口记录日志
    @LogAnnotation(module="文章",operator="获取文章列表")
    @Cache(expire = 5 * 60 * 1000, name = "listArticle")
    public Result listArticle(@RequestBody PageParams pageParams){
        return articleService.listArticle(pageParams);
    }

    /**
     * 首页最热文章
     *
     * @return
     */
    @ApiOperation(value = "获取 - 最热文章列表")
    @PostMapping("hot")
    @Cache(expire = 5 * 60 * 1000, name = "hot_article")
    public Result hotArticle() {
        int limit = 5;
        return articleService.hotArticle(limit);
    }

    /**
     * 首页最新文章
     * @return
     */
    @ApiOperation(value = "获取 - 最新文章列表")
    @PostMapping("new")
    @Cache(expire = 5 * 60 * 1000, name = "new_article")
    public Result newArticle(){
        int limit = 5;
        return articleService.newArticle(limit);
    }

    @ApiOperation(value = "获取 - 文章归档")
    @PostMapping("listArchives")
    public Result listArchives(){
        return articleService.listArchives();
    }

    @ApiOperation(value = "获取 - 文章")
    @PostMapping("view/{id}")
    public Result findArticleById(@PathVariable("id") Long articleId){
        return articleService.findArticleById(articleId);
    }

    @ApiOperation(value = "用户点赞")
//    @Cache(expire = 5 * 60 * 1000, name = "listArticle")
    @PostMapping("islike/{id}/{choice}")
    public Result islike(@PathVariable("id") Long articleId, @PathVariable("choice") Boolean choice){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        SysUser sysUser = loginUser.getSysUser();
        return articleService.islike(articleId, choice, sysUser);
    }

    @ApiOperation(value = "文章 - 发布")
//    @Cache(expire = 5 * 60 * 1000, name = "listArticle",updateRedis = true)
    @PostMapping("publish")
    public Result publish(@RequestBody ArticleParam articleParam){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        SysUser sysUser = loginUser.getSysUser();
        return articleService.publish(articleParam, sysUser);
    }

    @ApiOperation(value = "文章 - 查询")
    @PostMapping("{id}")
    public Result articleById(@PathVariable("id") Long articleId){
        return articleService.findArticleById(articleId);
    }

    @ApiOperation(value = "文章 - 删除")
    @Cache(expire = 5 * 60 * 1000, name = "listArticle",updateRedis = true)
    @DeleteMapping ("delete/{id}")
    @PreAuthorize("hasAuthority('dev:user')")
    public Result deleteArticleById(@PathVariable("id") Long articleId){
        return articleService.deleteArticleById(articleId);
    }

}
